package com.bluetooth.demo.ui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bluetooth.demo.R;
import com.bluetooth.demo.event.StepEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class StepFragment extends Fragment {

    private View mCurrentView;
    private TextView tv_step;
    private TextView tv_calorie;

    public StepFragment() {
        // Required empty public constructor
    }

    public static StepFragment newInstance() {
        StepFragment fragment = new StepFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mCurrentView = inflater.inflate(R.layout.fragment_step, container, false);
        EventBus.getDefault().register(this);
        initView(mCurrentView);
        return mCurrentView;
    }



    private void initView(View mCurrentView) {
        tv_step = (TextView) mCurrentView.findViewById(R.id.tv_step);
        tv_calorie = (TextView) mCurrentView.findViewById(R.id.tv_calorie);
    }


    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(StepEvent event) {
        double xx=event.getCalorie();
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
        df.format(xx);
        tv_step.setText("步数:" + event.getStep());
        tv_calorie.setText("能量:" + xx);
    }
}
